package com.tanhua.dubbo.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.Query;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.tanhua.dubbo.mappers.UserMapper;
import com.tanhua.model.domain.User;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@DubboService
public class UserApiImpl implements UserApi {
    @Autowired
   private UserMapper userMapper;

    @Override
    public User findByMobile(String mobile) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getMobile,mobile);
        User user = userMapper.selectOne(queryWrapper);
        return user;
    }

    @Override
    public Long save(User user) {
        userMapper.insert(user);
        return user.getId();
    }

    @Override
    public void updatePhone(Long userId, String phone) {
        //先查询当前用户得数据
        User user = userMapper.selectById(userId);
        user.setMobile(phone);
        userMapper.updateById(user);
    }

    @Override
    public void update(User user) {
        userMapper.updateById(user);
    }

    @Override
    public List<User> findAll() {
        List<User> users = userMapper.selectList(Wrappers.emptyWrapper());
        return users;
    }

    @Override
    public User findById(Long userId) {
        User user = userMapper.selectById(userId);
        return user;
    }

    /**
     * 根据环信ID查询用户信息
     * @param huanxinId
     * @return
     */
    @Override
    public User findByhuanxinId(String huanxinId) {
        QueryWrapper<User> qw = new QueryWrapper<>();
        qw.eq("hx_user",huanxinId);
        return userMapper.selectOne(qw);
    }
}
